<!--
 * @Description: 
 * @Author: 胡涛
 * @Date: 2023-09-11 21:21:30
 * @LastEditors: 胡涛
 * @LastEditTime: 2023-11-12 14:30:01
-->
<template>
  <div class="page-wrapper">
    <div class="toolbar">
      <div class="breadcrumb">
        <a-breadcrumb>
          <a-breadcrumb-item>组织架构</a-breadcrumb-item>
          <a-breadcrumb-item>行政区域</a-breadcrumb-item>
        </a-breadcrumb>
      </div>
      <a-space>
        <a-button type="primary" @click="onHandleCreate" v-auth="'Create'">
          <template #icon>
            <PlusOutlined />
          </template>
          添加区域
        </a-button>
        <a-button type="primary" :loading="loading" @click="queryList">
          <template #icon>
            <SyncOutlined />
          </template>
          刷新
        </a-button>
        <!-- <a-button type="primary">
          <template #icon>
            <ExportOutlined />
          </template>
          导出
        </a-button> -->
      </a-space>
    </div>
    <div class="container">
      <div class="table table-tree">
        <a-table
          bordered
          :columns="columns"
          :row-key="(record) => record.id"
          :data-source="dataSource"
          :pagination="false"
          :loading="loading"
          @change="handleTableChange"
        >
        </a-table>
      </div>
    </div>
  </div>
  <EditView
    v-model:visible="popupModel.visible"
    :action="popupModel.action"
    :id="popupModel.id"
    :categoryItems="categoryItems"
    v-if="popupModel.visible"
    @success="queryList"
  />
</template>

<script lang="ts" setup>
import {
  PlusOutlined,
  ExportOutlined,
  SyncOutlined,
  EditOutlined,
  DeleteOutlined,
} from "@ant-design/icons-vue";
import { useListPage } from "@/helpers/pages";
import { checkActionStyle } from "@/helpers";
import api from "@/api/system/region";
import { RegionTreeDto } from "@/api/system/types";
import { h, onMounted, ref } from "vue";
import { PaginationConfig } from "ant-design-vue/es/pagination";
import { enumName, enums } from "@/plugins/enums";
import {
  Button,
  ButtonGroup,
  message,
  Popconfirm,
  Switch,
} from "ant-design-vue";
import EditView from "./Edit.vue";

const {
  loading,
  dataSource,
  columns,
  pagination,
  popupModel,
  createQueryModel,
  onHandleCreate,
  onHandleUpdate,
} = useListPage<RegionTreeDto, string>({
  columns: [
    { dataIndex: "name", title: "区域名称", width: 300 },
    {
      dataIndex: "level",
      title: "区域级别",
      width: 100,
      align: "center",
      customRender({ value }) {
        return enumName(enums.RegionLevel, value);
      },
    },
    { dataIndex: "id", title: "行政区域代码", width: 300 },
    { dataIndex: "fullName", title: "完整名称", width: 300 },
    {
      dataIndex: "status",
      title: "启用状态",
      align: "center",
      customRender({ value }) {
        return h(Switch, { size: "small", disabled: true, checked: value });
      },
    },
    {
      dataIndex: "action",
      title: "操作",
      align: "center",
      width: 100,
      customRender: ({ record }: any) => {
        return h(ButtonGroup, [
          h(
            Button,
            {
              type: "primary",
              size: "small",
              ghost: true,
              style: checkActionStyle("Update"),
              onClick() {
                onHandleUpdate(record);
              },
            },
            {
              icon() {
                return [h(EditOutlined)];
              },
              default: () => {
                return "修改";
              },
            }
          ),
          // h(
          //   Popconfirm,
          //   {
          //     title: "删除提示",
          //     description: "确定要删除当前选择行数据信息吗？",
          //     onConfirm: () => {
          //       onDeleteItem(record);
          //     },
          //   },
          //   {
          //     default: () => {
          //       return h(
          //         Button,
          //         {
          //           danger: true,
          //           size: "small",
          //         },
          //         {
          //           icon() {
          //             return [h(DeleteOutlined)];
          //           },
          //           default: () => {
          //             return "删除";
          //           },
          //         }
          //       );
          //     },
          //   }
          // ),
        ]);
      },
    },
  ],
});
const categoryItems = ref<Array<RegionTreeDto>>([]);
const onDeleteItem = async (item: RegionTreeDto) => {
  let callback = message.loading("数据操作中，请稍后");
  await api.delete(item.id);
  callback();
  await queryList();
};

const queryList = async () => {
  loading.value = true;
  let items = await api.getTreeList(createQueryModel());
  dataSource.value = items;
  categoryItems.value = [{ name: "一级分类", id: null } as any, ...items];
  loading.value = false;
};

const handleTableChange = (pageConfig: PaginationConfig, filters: any) => {
  pagination.current = pageConfig.current;
  queryList();
};

onMounted(() => {
  queryList();
});
</script>

<style lang="less" scoped></style>
